<template>
  <div>
    <h2>User List</h2>
    <ul>
      <li v-for="user in users" :key="user.id">
        {{ user.username }} - {{ user.email }}
        <button @click="editUser(user)">Edit</button>
        <button @click="deleteUser(user.id)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
import userService from '../services/userService';

export default {
  data() {
    return {
      users: []
    };
  },
  created() {
    this.loadUsers();
  },
  methods: {
    loadUsers() {
      userService.getAllUsers()
        .then(response => {
          this.users = response.data;
        })
        .catch(error => {
          console.error('Error loading users:', error);
        });
    },
    editUser(user) {
      this.$emit('edit-user', user);
    },
    deleteUser(userId) {
      userService.deleteUser(userId)
        .then(() => {
          this.loadUsers(); // Reload the list after deleting
        })
        .catch(error => {
          console.error('Error deleting user:', error);
        });
    }
  }
};
</script>
